<?php

FLEA::loadClass('Controller_Public');
include('OpXML.php');
class Controller_MngrAlbum extends Controller_Public
{	
	var $tableAlbums;
	function Controller_MngrAlbum()
    {
        $this->tableAlbums = FLEA::getSingleton('Model_Albums');
	}
	
	function actionList()
	{
		if (isset($_GET['result'])) {
            $result = (int)$_GET['result'];
        }

		$args = $_GET['args'];
		
		if ($result > 0) {
			if ( $args == 'add' )
				$message = "<div id=\"message\" class=\"updated fade\"><p><strong>专辑添加成功。</strong> </p></div>";
			else if ($args == 'edit')
				$message = "<div id=\"message\" class=\"updated fade\"><p><strong>专辑修改成功。</strong> </p></div>";
		} else {
			$message = '';
		}
		

		//$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
		

		//load_class('FLEA_Helper_Pager');
		//$pager =& new FLEA_Helper_Pager($this->tableAlbums, $page,	 $pageSize, $conditions, $sort);
		//$rowset = $pager->findAll();
		
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

		FLEA::loadClass('Controller_Pager');
		$pager = &new Controller_AdminPager($this->tableAlbums, $page-1,FLEA::getAppInf('pageSize'),$condition,'update_date DESC');
		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('albumList', $pager->findAll());
		$smarty->assign('page', $page);
		$smarty->assign('message', $message);	
        $smarty->assign('page_info',$pager->getNavBar(url('mngralbum','list')) );
		$smarty->display($this->getTemplates());
	}
	
	function actionAdd()
	{
		FLEA::loadHelper('array');

		//乐队下拉列表
		$tableArtists =& FLEA::getSingleton('Model_Artists');		
		$rowset = $tableArtists->findAll(null, 'artist_id ASC');		
		$artists = array_to_hashmap($rowset, 'artist_name', 'artist_id');

		//厂牌下拉列表
		$tableLabels =& FLEA::getSingleton('Model_Labels');		
		$rowset = $tableLabels->findAll(null, 'label_id ASC');
		$labels = array_to_hashmap($rowset, 'label_name', 'label_id');

		$tableStyles =& FLEA::getSingleton('Model_Styles');	
		$condition = 'style_id<>0';
		$styles = $tableStyles->findAll($condition, 'style_id ASC');

		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('artists', $artists);
		$smarty->assign('labels', $labels);
		$smarty->assign('styles', $styles);
		$smarty->display($this->getTemplates());
	}

	function actionAdded()
	{
		// 处理上传文件
        FLEA::loadHelper('uploader');
        $uploader =& new FLEA_Helper_FileUploader();
        if (!$uploader->isFileExist('album_thumb') || !$uploader->isFileExist('album_photo')) {
            js_alert('请选择要上传的文件', '', url('mngralbum', 'add') );
        }
        // 检查上传文件的扩展名是否正确
        $thumbfile = $uploader->getFile('album_thumb');
		$photofile = $uploader->getFile('album_photo');
		
        if (!$thumbfile->check('.jpg/.png/.gif') || !$photofile->check('.jpg/.png/.gif')) {
            js_alert('只允许上传 jpeg/gif/png 格式的文件', '', url('mngralbum', 'add'));
        }

        $ret = $this->tableAlbums->uploadPhoto(null,$thumbfile,$photofile);

		
		redirect(url('mngralbum', 'list',array('result' => $ret,'args'=>'add')));
		
	}

	function actionEdit()
	{
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

		FLEA::loadHelper('array');

		if (isset($_GET['album_id'])) {
            $album_id = (int)$_GET['album_id'];
        }

		$condition = 'album_id = '.$album_id;
		$album = $this->tableAlbums->find($condition);
		$tableArtists =& FLEA::getSingleton('Model_Artists');		
		$artists = $tableArtists->findAll(null, 'artist_id ASC');

		$tableLabels =& FLEA::getSingleton('Model_Labels');		
		$labels = $tableLabels->findAll(null, 'label_id ASC');

		$tableStyles =& FLEA::getSingleton('Model_Styles');		
		$condition = 'style_id<>0';
		$styles = $tableStyles->findAll($condition, 'style_id ASC');

		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('album', $album);
		$smarty->assign('artists', $artists);
		$smarty->assign('labels', $labels);
		$smarty->assign('styles', $styles);
		$smarty->assign('page', $page);
		$smarty->assign('selStyle', array_to_hashmap($album['style'],'style_name','style_id'));
		$smarty->display($this->getTemplates());
	}

	function actionUpdate()
	{
		if (isset($_POST['album_id'])) {
            $album_id = (int)$_POST['album_id'];
        }

		$page = isset($_POST['page']) ? (int)$_POST['page'] : 1;
		// 处理上传文件
        FLEA::loadHelper('uploader');
        $uploader =& new FLEA_Helper_FileUploader();

		if ($uploader->isFileExist('album_thumb') && $uploader->isFileExist('album_photo')) {
			$thumbfile = $uploader->getFile('album_thumb');
			$photofile = $uploader->getFile('album_photo');
		}

        $ret = $this->tableAlbums->uploadPhoto($album_id,$thumbfile,$photofile);
		//$this->_goBack();
		redirect(url('mngralbum', 'list',array('result' => $ret,'args'=>'edit','page'=>$page)));
	}
	
		
	function actionSongs()
	{

		$this->_setBack();

		if (isset($_GET['album_id'])) {
            $album_id = (int)$_GET['album_id'];
        }

		$fpath = dirname(dirname(dirname(__FILE__))).'./playlist/'.$album_id.'.xml';
		
		if (!file_exists($fpath))
		{
			$fpath = '未生成Playlist!';
		}else
		{
			$fpath = FLEA::getAppInf('webAddr').'/playlist/'.$album_id.'.xml';
		}

		$condition = 'album_id = '.$album_id;
		$album = $this->tableAlbums->find($condition);
		$songs = $album[songs];

		$smarty = &$this->_getView();
		$smarty->assign('menuName', $this->getMenuName());
		$smarty->assign('songList', $songs);
		$smarty->assign('album_id', $album_id);
		$smarty->assign('playlist', $fpath);
		$smarty->display('mngralbum_songs.html');
	}

	function actionSAdd()
	{

		if (isset($_POST['album_id'])) {
            $album_id = (int)$_POST['album_id'];
			$data = array(
				'album_id' => $album_id,
				'song_seq' => $_POST['song_seq'],
				'song_title'=>$_POST['song_title'],
				'song_lyric'=>$_POST['song_lyric'],
			);
			//print_r($this->artist);
			//dump($data);
			$tableSongs = FLEA::getSingleton('Model_Songs');
			$tableSongs->create($data);
        }	

		$this->_goBack();		
	}

	function actionSDelete()
	{
		if (isset($_GET['sid'])) {
            $song_id = (int)$_GET['sid'];
			$condition = 'song_id='.$song_id;
			$tableSongs = FLEA::getSingleton('Model_Songs');
			$tableSongs->removeByConditions($condition);
        }	

		$this->_goBack();		
	}

	function actionSUpdate()
	{
		if (isset($_POST['album_id'])) {
            $album_id = (int)$_POST['album_id'];
			$data = array(
				'song_id' =>$_POST['song_id'],
				'album_id' => $album_id,
				'song_seq' => $_POST['song_seq'],
				'song_title'=>$_POST['song_title'],
				'song_lyric'=>$_POST['song_lyric'],
			);
			$tableSongs = FLEA::getSingleton('Model_Songs');
			$tableSongs->save($data);
        }	

		$this->_goBack();		
	}

	function actionSPlaylist()
	{
		if (isset($_GET['album_id'])) {
            $album_id = (int)$_GET['album_id'];
			
			$condition = 'album_id = '.$album_id;
			$album = $this->tableAlbums->find($condition);
			$xmlImage = FLEA::getAppInf('webAddr').'/upload/photos/'.$album['album_ori'];
			//$condition = 'album_id = '.$album_id;
			$tableSongs = FLEA::getSingleton('Model_Songs');
			$songs = $tableSongs->findAll($condition,'song_seq ASC');

			foreach ($songs as $key=>$value)
			{
				$arr[]=array('title'=>$value['song_title'],'location'=>FLEA::getAppInf('webAddr').'/upload/mp3/'.$album_id.'/'.sprintf("%02d",$value['song_seq']).'.mp3','image'=>$xmlImage);
			}


        }	
		$rootname='trackList';
		$fpath=dirname(dirname(dirname(__FILE__))).'./playlist/'.$album_id.'.xml';
		$x=new OpXML('trackList',$fpath);

		
		$x->insertM($arr);
		

		$data = array(				
				'album_id' => $album_id,
				'update_date' => date('Y-m-d H:i:s'),				

			);
			//dump($data);
			$tableAlbums = FLEA::getSingleton('Model_Albums');
			$tableAlbums->save($data);	

		$this->_goBack();		
	}
} 